//
//  FavTabController.m
//  FavClient
//
//  Created by MagicStudio on 12-4-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "FavTabController.h"
#import "FavTabDetailController.h"
#import "FavHelper.h"


@implementation FavTabController

@synthesize mTableView ;
@synthesize mImageView ;
@synthesize detailController ;
@synthesize initDetailProgressDialog ;

-(void) headerImageClick{
    [UIViewAnimationHelper doCATAnimation:self.mImageView duration:1.0f type:Type_RippleEffect] ;
}

#pragma Mark-

//当点击Item时,加载页面时初始化数据，开启一个线程初始化
-(void) initData {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMutableArray * array = [[NSMutableArray alloc] init] ;
    for(int i = 0 ; i < 10 ; i ++){
        UIImage *img = [UIImage imageNamed:@"fav_tab_detail.jpg"] ;
        [array addObject:img] ;
    }
    [NSThread sleepForTimeInterval:1.5f] ;
    [self performSelectorOnMainThread:@selector(initComplete:) withObject:array waitUntilDone:YES] ;
    [pool release] ;
}

//初始化完成,加载完成后切换到另一个页面
-(void) initComplete : (id) array{
    [self.initDetailProgressDialog dismissWithClickedButtonIndex:0 animated:YES] ;
    FavTabDetailController *detail = (FavTabDetailController *)[[FavTabDetailController alloc] initWithNibName:@"FavTabDetailController" bundle:nil] ;
    self.detailController = detail ;
    self.detailController.arrayImage = array ;
    CATransition *anim = [UIViewAnimationHelper getCATAnimation:0.7f type:Type_PageCurl subType:kCATransitionFromRight] ;
    [ViewChangeHelper changeControllerForSelf:self.detailController disAppearController:self catTrasition:anim] ;
    // [title release] ;
    [detail autorelease] ;
}

#pragma Mark-

- (void)dealloc
{
    [mTableView release] ;
    [mImageView release] ;
    [detailController release] ;
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    mTableView.backgroundColor = [UIColor clearColor] ;
    self.mImageView.userInteractionEnabled = YES ;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageClick)] ;
    [self.mImageView addGestureRecognizer:singleTap] ;
    [singleTap release] ;
    [super viewDidLoad];
}



#pragma Mark- UITableDataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10 ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *FAVTABCELL = @"FAVTABCELL" ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FAVTABCELL] ;
    if(cell == nil){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"FavTabCell" owner:self options:nil] objectAtIndex:0] ;
    }
    return cell ;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return 0 ;
}    // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return 0 ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40 ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.initDetailProgressDialog = [UIAlertViewHelper showProgressDialog:@"正在加载数据,请等待" delegate : self alertSize : CGSizeMake(180, 180)] ;
    [NSThread detachNewThreadSelector:@selector(initData) toTarget:self withObject:nil] ;
}


#pragma Mark- UIAlertViewDelegate Method
- (void)willPresentAlertView:(UIAlertView *)alertView{
    alertView.frame = CGRectMake(70, 150, 180, 180) ;
}

@end
